package com.one.consumer.main.ui.fragment;

import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.imageloader.LoaderManager;
import com.one.common.model.bean.BannerBean;
import com.one.common.view.base.BaseFragment;
import com.one.consumer.R;
import com.one.consumer.main.presenter.HomePresenter;
import com.one.consumer.main.ui.view.BannerView;
import com.one.common.common.order.extra.ReserveExtra;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.one.common.model.http.NetConstant.IMG_HOST;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/21 上午10:45
 * description ：
 **/
public class HomeFragment extends BaseFragment<HomePresenter> implements BannerView {
    @BindView(R.id.banner)
    ConvenientBanner banner;

    @Override
    protected void initPresenter() {
        mPresenter = new HomePresenter(this,getActivity());
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getBanner();
//        mPresenter.getMainInfo();
    }


    /**
     * 一键保修
     */
    @OnClick(R.id.ll_home_1)
    public void click1(){
        RouterManager.getInstance().go(RouterPath.ADD_SERVICE);
    }

    /**
     * 设计预约
     */
    @OnClick(R.id.ll_home_2)
    public void click2(){
        RouterManager.getInstance().go(RouterPath.ADD_RESERVE_DE,new ReserveExtra(ReserveExtra.STYLE_DY));
    }

    /**
     * 装修预约
     */
    @OnClick(R.id.ll_home_3)
    public void click3(){
        RouterManager.getInstance().go(RouterPath.DE_RESERVE,new ReserveExtra(ReserveExtra.STYLE_DE));
    }

    /**
     * 定制加配
     */
    @OnClick(R.id.ll_home_4)
    public void click4(){
        RouterManager.getInstance().go(RouterPath.CUSTOM_PROJECT);
    }


    @Override
    public void setBanner(ArrayList<BannerBean> banners) {
        banner.setPages(
                new CBViewHolderCreator() {
                    @Override
                    public LocalImageHolderView createHolder(View itemView) {
                        return new LocalImageHolderView(itemView);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.layout_banner;
                    }
                }, banners)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        // TODO banner点击事件
                    }
                });
    }

    @Override
    public void setMainInfo(String res) {

    }

    public class LocalImageHolderView extends Holder<BannerBean> {

        ImageView imageView;

        public LocalImageHolderView(View itemView) {
            super(itemView);
        }

        @Override
        protected void initView(View itemView) {
            imageView = (ImageView) itemView;
        }

        @Override
        public void updateUI(BannerBean data) {

            LoaderManager.getLoader().loadNet(imageView, IMG_HOST+data.getBanner_img());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (banner != null && !banner.isTurning()) {
            banner.startTurning(2000);
        }
    }
    @Override
    public void onPause() {
        super.onPause();
        if (banner != null) {
            banner.stopTurning();
        }
    }



}
